# 给定一个正整数 n，生成一个包含 1 到 n2 所有元素，且元素按顺时针顺序螺旋排列的正方形矩阵。


class Solution:
    def generateMatrix(self, n: int):
        res_list = []
        for i in range(n):
            res_list.append(list("*" * n))
        dir_flag = "you"
        i, j, num = 0, 0, 1
        while num <= n ** 2:
            if dir_flag == "you":
                if j < n and res_list[i][j] == "*":
                    res_list[i][j] = num
                    j += 1
                    num += 1
                else:
                    j -= 1
                    i += 1
                    dir_flag = "xia"
            if dir_flag == "xia":
                if i < n and res_list[i][j] == "*":
                    res_list[i][j] = num
                    i += 1
                    num += 1
                else:
                    i -= 1
                    j -= 1
                    dir_flag = "zuo"
            if dir_flag == "zuo":
                if j >= 0 and res_list[i][j] == "*":
                    res_list[i][j] = num
                    j -= 1
                    num += 1
                else:
                    j += 1
                    i -= 1
                    dir_flag = "shang"
            if dir_flag == "shang":
                if i >= 0 and res_list[i][j] == "*":
                    res_list[i][j] = num
                    i -= 1
                    num += 1
                else:
                    i += 1
                    j += 1
                    dir_flag = "you"
            # print(res_list)
        return res_list


a = Solution()
print(a.generateMatrix(4))
